﻿using CRM.Admin.Contracts.Contract.Customer;
using CRM.Admin.Contracts.Dto.Customer;
using CRM.Admin.Contracts.Model.Customer;
using KingsSharp.Common;
using KingsSharp.Common.Extensions;
using KingsSharp.Common.Filter;
using KingsSharp.Core.Data.Entity;
using KingsSharp.Core.Data.Extensions;
using KingsSharp.Web.UI;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace CRM.Admin.Areas.Customer.Controllers
{
    public class AscriptionController : Controller
    {
        /// <summary>
        /// 顾客归属业务契约
        /// </summary>
        public IAscriptionService AscriptionService { get; set; }

        #region 视图功能
        /// <summary>
        /// 列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult AscriptionIndex()
        {
            return View();
        }

        /// <summary>
        /// 新增页
        /// </summary>
        /// <returns></returns>
        public ActionResult AscriptionAdd(Guid CustomerId)
        {
            ViewBag.CustomerId = CustomerId;
            return View();
        }
        /// <summary>
        /// 编辑页
        /// </summary>
        /// <param name="AscriptionId"></param>
        /// <returns></returns>
        public ActionResult AscriptionEdit(Guid AscriptionId)
        {
            var dto = AscriptionService.Item.Where(a => a.Id == AscriptionId).FirstOrDefault().MapTo<AscriptionDto>();
            return View(dto);
        }

        #endregion

        #region 数据功能
        public ActionResult AscriptionInsert(AscriptionDto dto)
        {
            dto.CheckNotNull(nameof(dto));
            dto.Id = Guid.NewGuid();
            var result = AscriptionService.Insert(dto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "顾客归属记录添加成功。";
            }
            return Json(new AjaxResult(msg, msgType));
        }
        public ActionResult AscriptionUpdate(AscriptionDto dto)
        {
            dto.CheckNotNull(nameof(dto));
            var result = AscriptionService.Update(dto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "顾客归属记录更新成功。";
            }
            return Json(new AjaxResult(msg, msgType));
        }
        public ActionResult AscriptionDelete(Guid UserId)
        {
            var result = AscriptionService.Delete(UserId);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "删除成功。";
            }
            return Json(new AjaxResult(msg, msgType));
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="dtos"></param>
        /// <returns></returns>
        public ActionResult GetAscriptionGridData()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<Ascription, bool>> predicate = FilterHelper.GetExpression<Ascription>(request.FilterGroup);
            int total;
            var data = AscriptionService.Item.Include(a => a.Organization)
                .Where(a => a.IsDeleted == false)
                .Where<Ascription, Guid>(predicate, request.PageCondition, out total)
                .Select(s => new
                {
                    s.Id,
                    s.CustomerId,
                    s.Organization.Name,
                    s.OrganizationId,
                    s.DeputyName,
                    s.CreatedTime
                }).ToList();
            return Json(new GridData<object>(data, total), JsonRequestBehavior.AllowGet);
        }

        #endregion

    }
}